package ca.esdot.ane.intent.functions;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;

import com.adobe.fre.FREArray;
import com.adobe.fre.FREContext;
import com.adobe.fre.FREFunction;
import com.adobe.fre.FREObject;
import ca.esdot.ane.intent.IntentContext;

public class SendIntentFunction implements FREFunction {

	IntentContext context;
	
	@Override
	public FREObject call(FREContext context, FREObject[] params) {
		this.context = (IntentContext) context;
		
		FREObject result = null;
		
		try {
			String action = params[0].getAsString();
			String url = params[1].getAsString();
			String extraName = "sms_body";
			String extraContent = "TEst SMS!";
			
			FREArray extras = (FREArray) params[2];
			
			Activity activity = context.getActivity();
			
			Intent intent;
			if(url != null){
				intent = new Intent(action, Uri.parse(url));
			} else {
				intent = new Intent(action);
			}
			if(extraName != null && extraContent != null){
				intent.putExtra(extraName, extraContent);
			}
			activity.startActivity(intent);
			
		} 
		catch(Exception e){
			e.printStackTrace();
		}
		
		return result;
	}

}
